package com.wanxi.servlet.newstype;

import com.alibaba.fastjson.JSONObject;
import com.wanxi.dto.ResultDto;
import com.wanxi.model.NewsTypeModel;
import com.wanxi.service.Impl.NewsTypeServiceImpl;
import com.wanxi.service.NewsTypeService;
import com.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Li
 * @Date 2023/5/7 20:24
 */
@WebServlet("/back/AddNewsType")
public class AddNewsTypeServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      //乱码处理
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //接收前端值
        String newType = req.getParameter("newType");
        String enable = req.getParameter("enable");
        String remark = req.getParameter("remark");
        //封装model
        NewsTypeModel newsTypeModel = NewsTypeModel.builder()
                .newType(Tool.nullToStr(newType))
                .enable(Tool.nullToInt(enable))
                .remark(Tool.nullToStr(remark))
                .build();
        //调用service层
        NewsTypeService newsTypeService = new NewsTypeServiceImpl();
        ResultDto resultDto = newsTypeService.add(newsTypeModel);
        //返回给前端
        resp.getWriter().println(JSONObject.toJSONString(resultDto));
    }
}
